//
//  LBGraphics.h
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/8/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

/** 
NOTE : Graphics must be setView and setDefaultFrameBuffer() before use.
 */
#ifndef OpenGLGame_LBGraphics_h
#define OpenGLGame_LBGraphics_h

#include "LBStruct.h"
#include <GLKit/GLKit.h>

class LBProgram;
class LBTexture;
class LBFramebuffer;
class Matrix;

class LBGraphics{
private:
    LBProgram* defaultProgram;
    LBProgram* drawProgram;
    LBProgram* program;
    static LBGraphics* instance;
    int width;                                                      //use for map texture, in logic
    int height;
    int xScreenViewport, yScreenViewport, widthScreenViewport, heightScreenViewport;     //screen view port in device
    int widthScreen, heightScreen;                                  //screen size of device
    
    GLuint positionAttribute;                                       //default position attribute, texCoor, texture, use to render texture (or when use default program).
    GLuint texCoorAttribute;
    GLuint textureUniform;
    
    GLint defaultFB;                                                //default frame buffer in real device
    
    char vShaderStrDefault[1000];                                   //default shader string
    char fShaderStrDefault[1000];
    
    Matrix *mProjection;
    Matrix *mWorld;
    
    LBGraphics();
    
    float rectVertices[10];
    float rectColors[4];

public:
    static LBGraphics* getInstance();
    virtual ~LBGraphics();
    
    void init();                                                    //HAVE TO CALL BEFORE USE OTHER FUNCTION
    void useProgram(LBProgram* program);
    //float mapXCoorFromScreenToOpenGL(float x);
    //float mapYCoorFromScreenToOpenGL(float y);
    
    void deleteInstance();
    void useDefaultProgram();
    
    GLuint attributeIndex(const char* attributeName);
    GLuint uniformIndex(const char* uniformName);
    
    LBTexture* changeTempTexture();
    void generateNewTempTextures(int width, int height);
    void viewport(int x, int y, int width, int height);
    void clear(float red, float green, float blue);
    
    //getter and setter
    LBProgram* getDefaultProgram();
    int getWidth();
    int getHeight();
    GLuint getPositionAttribute();
    GLuint getTexCoorAttribute();
    GLuint getTextureUniform();
    GLuint getWorldUniform();
    GLuint getProjectionUniform();
    char* getDefaultVShaderStr();
    char* getDefaultFShaderStr();
    LBProgram* getProgram();
    LBTexture* getCurrentTempTexture();
    LBFramebuffer* getCurrentTempFramebuffer();
    GLint getDefaultFrameBuffer();
    Matrix* getProjectionMatrix();
    
    void setWindowsSize(int width, int height);
    void setScreenSizeAndViewportInRealDevice(int widthScreen, int heightScreen, int xScreenViewport, int yScreenViewport, int widthScreenViewport, int heightScreenViewport);
    void setDefaulFrameBuffer(GLint framebuffer);                   //HAVE TO CALL BEFORE USE OTHER FUNCTION
    void renderToScreen();
    
    void setRectColor(float red, float green, float blue, float alpha);
    void drawRect(LBRect rect);
};

#endif
